

import sys
import os

def main():
	global path

	path = sys.argv[1]
	files = os.listdir(path)

	for nsfile in files:
		print nsfile
		sortByYear(path+nsfile) 


def sortByYear(fileName):
	fileVar = open(fileName,"r") #opens the prelim file
	openDict = {}


	for line in fileVar:
		pieces = line.split('\t')
		name = pieces[2].strip() #namespace
		startYear = pieces[3].split('-') #obtains start year
		fileName2 = path + "years/"+name+startYear[0]+".txt" #creates new file with start year of editor and namespace
		if fileName2 not in openDict:
			fileVar2 = open(fileName2,"a")
			openDict[fileName2] = fileVar2
		else:
			fileVar2 = openDict[fileName2]
		
		fileVar2.write(line) 
		
	for fileVar2 in openDict.values():
		fileVar2.close() 
	
	fileVar.close()

main()
